package com.lysj.bhp.api.service;

import com.google.gson.Gson;
import com.lysj.bhp.api.base.ApiContent;
import com.lysj.bhp.exchangerate.json.bcex.*;
import com.lysj.bhp.util.http.HttpUtil;
import com.lysj.bhp.util.random.RandomUtil;
import com.lysj.bhp.util.web.ParamUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author zk
 * @description
 * @create 2018-07-25 10:22
 **/
@Service
public class BcexproxyService extends ApiContent {

    private static Logger logger = LoggerFactory.getLogger(BcexproxyService.class);

    private static String BCEX_BTC_TO_CNY_URL="http://120.77.85.235:8098/bcex/btcCny";
    private static String BCEX_MARKETS_URL="http://120.77.85.235:8098/bcex/markets";
    /**
     * 获取以太坊币与美元的汇率
     *
     * @return
     */
    public BigDecimal getEthereumToDollar() {
        long time = exchangeRate.getETHEREUM_TO_DOLLAR_EXPIRES();
        BigDecimal rate = exchangeRate.getETHEREUM_TO_DOLLAR();
        if (System.currentTimeMillis() < time) { //汇率还在有效期内

            return rate;
        }

        String respJson = HttpUtil.sendGet(BCEX_MARKETS_URL, "");
        if (ParamUtil.isBlack(respJson)) {
            return rate;
        }
        BcexResp bcexResp = new Gson().fromJson(respJson, BcexResp.class);
        if (bcexResp == null) {
            logger.error("Json格式化错误:" + respJson);
            return rate;
        }
        if (bcexResp.getCode() != 0) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }
        Data data = bcexResp.getData();
        if (data == null) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }

        List<Ckusd> ckusdList = data.getCkusd();
        if (ckusdList == null || ckusdList.size() == 0) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }
        for (Ckusd ckusd : ckusdList) {
            if ("eth".equalsIgnoreCase(ckusd.getCoin_from())) {
                BigDecimal ethToDollar = new BigDecimal(ckusd.getPreprice());
                logger.info(ethToDollar + "以太坊对美元汇率----");
                rate = ethToDollar;
            }
        }
        long expiresRandom=RandomUtil.getRandomNum(min,max);
        exchangeRate.setETHEREUM_TO_DOLLAR_EXPIRES(System.currentTimeMillis() + expiresRandom);
        return rate;
    }


    /**
     * 获取比特币与美元的汇率
     *
     * @return
     */
    public BigDecimal getBitcoinToDollar() {
        long time = exchangeRate.getBITCOIN_TO_DOLLAR_EXPIRES();
        BigDecimal rate = exchangeRate.getBITCOIN_TO_DOLLAR();
        if (System.currentTimeMillis() < time) { //汇率还在有效期内

            return rate;
        }

        String respJson = HttpUtil.sendGet(BCEX_MARKETS_URL, "");
        if (ParamUtil.isBlack(respJson)) {
            return rate;
        }
        BcexResp bcexResp = new Gson().fromJson(respJson, BcexResp.class);
        if (bcexResp == null) {
            logger.error("Json格式化错误:" + respJson);
            return rate;
        }
        if (bcexResp.getCode() != 0) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }
        Data data = bcexResp.getData();
        if (data == null) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }

        List<Ckusd> ckusdList = data.getCkusd();
        if (ckusdList == null || ckusdList.size() == 0) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }
        for (Ckusd ckusd : ckusdList) {
            if ("btc".equalsIgnoreCase(ckusd.getCoin_from())) {
                BigDecimal btcToDollar = new BigDecimal(ckusd.getPreprice());
                logger.info(btcToDollar + "比特币对美元汇率----");
                rate = btcToDollar;
            }
        }
        long expiresRandom=RandomUtil.getRandomNum(min,max);
        exchangeRate.setBITCOIN_TO_DOLLAR_EXPIRES(System.currentTimeMillis() + expiresRandom);
        return rate;
    }

    /**
     * @author drj
     * @date 2018/05/30 20:06
     * @Description:获取美元与人民币的汇率
     */
    public BigDecimal dollarToCnyRate() {
        long time = exchangeRate.getDOLLAR_TO_CNY_EXPIRES();
        BigDecimal rate = exchangeRate.getDOLLAR_TO_CNY();
        if (System.currentTimeMillis() < time) { //汇率还在有效期内
            return rate;
        }
        String respJson = HttpUtil.sendGet(BCEX_BTC_TO_CNY_URL, "");
        if (ParamUtil.isBlack(respJson)) {
            return rate;
        }
        BcexCnyResp bcexCnyResp = new Gson().fromJson(respJson, BcexCnyResp.class);
        if (bcexCnyResp == null) {
            logger.error("Json格式化错误:" + respJson);
            return rate;
        }
        String dollar2cny = bcexCnyResp.getUsd2cnyrate(); //获取比特币对人民币的汇率
        if (ParamUtil.isBlack(dollar2cny)) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }
        logger.info(dollar2cny + "美元对人民币的汇率----");
        rate = new BigDecimal(dollar2cny);
        long expiresRandom=RandomUtil.getRandomNum(min,max);
        exchangeRate.setDOLLAR_TO_CNY_EXPIRES(System.currentTimeMillis() + expiresRandom);
        return rate;
    }

    /**
     * 获取算力对以太坊的汇率
     *
     * @return
     */
    public BigDecimal getBhpcToEth() {
        long time = exchangeRate.getBHPC_TO_ETH_EXPIRES();
        BigDecimal rate = exchangeRate.getBHPC_TO_ETH();
        if (System.currentTimeMillis() < time) { //汇率还在有效期内
            return rate;
        }
        String respJson = HttpUtil.sendGet(BCEX_MARKETS_URL, "");
        if (ParamUtil.isBlack(respJson)) {
            return rate;
        }
        BcexResp bcexResp = new Gson().fromJson(respJson, BcexResp.class);
        if (bcexResp == null) {
            logger.error("Json格式化错误:" + respJson);
            return rate;
        }
        if (bcexResp.getCode() != 0) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }
        Data data = bcexResp.getData();
        if (data == null) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }
        List<Eth> eths = data.getEth();
        if (eths == null || eths.size() == 0) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }
        for (Eth eth : eths) {
            if ("bhpc".equalsIgnoreCase(eth.getCoin_from())) {
                rate = new BigDecimal(eth.getPreprice());
                logger.info(rate + "算力对以太坊汇率----");
            }
        }
        long expiresRandom=RandomUtil.getRandomNum(min,max);
        exchangeRate.setBHPC_TO_ETH_EXPIRES(System.currentTimeMillis() + expiresRandom);
        return rate;
    }


}
